package demo.swing.administrator.panel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import demo.swing.date.EnterDate;

public class BtnsListeners implements ActionListener {

	private enum TYPE_LISTENER{
		ADMIN_WINDOW_ACTION,
		ENTER_NEW_FLIGHT_WINDOW,
		ENTER_DATE,
		NONE
	}
	AdministratorPanel 	adminPabel = null;
	EnterNewFlight		enterNewFlight = null;
	EnterDate			enterDate = null;
	public BtnsListeners(AdministratorPanel p) {
		// TODO Auto-generated constructor stub
		adminPabel = p;
	}
	public BtnsListeners(EnterNewFlight f) {
		// TODO Auto-generated constructor stub
		enterNewFlight = f;
	}
	public BtnsListeners(EnterNewFlight f, EnterDate d){
		enterDate = d;
		enterNewFlight = f;
	}
	private TYPE_LISTENER getTypeListener(){
		if (adminPabel == null && enterNewFlight != null && enterDate == null){
			return TYPE_LISTENER.ENTER_NEW_FLIGHT_WINDOW;
		}else if (adminPabel != null && enterNewFlight == null && enterDate == null){
			return TYPE_LISTENER.ADMIN_WINDOW_ACTION;
		}else if(enterDate != null && enterNewFlight != null && adminPabel == null){
			return TYPE_LISTENER.ENTER_DATE;
		}
		return TYPE_LISTENER.NONE;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		switch (getTypeListener()){
			case ADMIN_WINDOW_ACTION:
				if (e.getSource() == adminPabel.getUpdateBtn()){
					try {
						adminPabel.updateTable();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}else if (e.getSource() == adminPabel.getInsertBtn()){
					adminPabel.insertFlightIntoTable();
				}else if (e.getSource() == adminPabel.getSearchBtn()){
					try {
						adminPabel.searchFlights();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}			
				break;
			case ENTER_NEW_FLIGHT_WINDOW:
				if (e.getSource() == enterNewFlight.getEnterDateBtn()){
					enterNewFlight.enterDate();
				}
				if (e.getSource() == enterNewFlight.getInsertNewFlightBtn()){
					enterNewFlight.insertNewFlight();
				}
				break;
			case ENTER_DATE:
				if (e.getSource() == enterDate.getOkBtn()){
					enterNewFlight.dataIsEntered(enterDate.getDate());
				}
			default:
				break;
			}
		}

}
